package scau.hotel.hotel_web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import scau.hotel.hotel_service.entity.Payment;
import scau.hotel.hotel_service.service.PaymentService;
import scau.hotel.hotel_model.controller.RespBean;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Payment)表控制层
 *
 * @author makejava
 * @since 2022-03-28 20:30:48
 */
@RestController
@PreAuthorize("hasAnyAuthority('admin','normal')")
@RequestMapping("payment")
@Api(value = "typeApi", tags = "支付控制器")
public class PaymentController {
    /**
     * 服务对象
     */
    @Resource
    private PaymentService paymentService;

    /**
     * 不分页查询
     *
     * @param payment 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("不分页查询所有")
    public RespBean queryAll(Payment payment) {
        return RespBean.success(this.paymentService.queryAll(payment));
    }

    /**
     * 分页查询
     *
     * @param pageNum  起始页
     * @param pageSize 每页数据大小
     * @param payment     筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public RespBean page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Payment payment) {
        PageHelper.startPage(pageNum, pageSize);
        List<Payment> list = paymentService.queryAll(payment);
        PageInfo<List> pageInfo = new PageInfo(list);
        return RespBean.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param paymentId 主键
     * @return 单条数据
     */
    @GetMapping("{paymentId}")
    @ApiOperation("根据id查询")
    public RespBean queryById(@PathVariable("paymentId") Integer paymentId) {
        return RespBean.success(this.paymentService.queryById(paymentId));
    }

    /**
     * 新增数据
     *
     * @param payment 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增一行数据")
    public RespBean add(@RequestBody Payment payment) {
        return this.paymentService.insert(payment);
    }

    /**
     * 编辑数据
     *
     * @param payment 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean edit(@RequestBody Payment payment) {
        return this.paymentService.update(payment);
    }

    /**
     * 删除数据
     *
     * @param paymentId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{paymentId}")
    @ApiOperation("根据id删除数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean deleteById(@PathVariable Integer paymentId) {
        Payment payment = new Payment();
        payment.setPaymentId(paymentId);
        payment.setDelFlag(0);
        return this.paymentService.update(payment);
    }

}

